package com.xiong.springsecurity.handle;

import com.alibaba.fastjson.JSON;
import com.xiong.springsecurity.domain.ResponseResult;
import com.xiong.springsecurity.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *
 * @author XiongDa
 * @version 1.0.0
 * @belongsProject: SpringSecurity
 * @belongsPackage: com.xiong.springsecurity.handle
 * @since 2023/6/15 18:37
 **/
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        log.info(e.getMessage());
//        ResponseResult<String> result = new ResponseResult<>(403, "认证失败, 请重新登入", null);
        ResponseResult<String> result = new ResponseResult<>(403, e.getMessage(), null);
        String s = JSON.toJSONString(result);
        WebUtils.renderString(httpServletResponse, s);
    }

}
